package com.pan.service.impl;

import com.pan.dao.Rc_districtSelectDao;
import com.pan.entity.Rc_district;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.service.DistrictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


@Service("districtService")
public class DistrictServiceImpl implements DistrictService {

    @Resource
    private Rc_districtSelectDao rc_districtSelectDao;

    @Override
    public Result<Rc_district> getDistrict(short district_id){
        Rc_district district = rc_districtSelectDao.selectByDistrict_id(district_id);
        if (district == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        Result<Rc_district> result = new Result<>(ResultEnum.SUCCESS,district);
        return result;
    }

    @Override
    public Result<String> getDistrictName(short district_id) {
        Rc_district district =rc_districtSelectDao.selectByDistrict_id(district_id);
        if (district == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        String districtName = district.getDistrict();
        Result<String> result = new Result<>(ResultEnum.SUCCESS,districtName);
        return result;
    }

    @Override
    public Result<List<Rc_district>> getChildDistrict(short district_id){
        Rc_district district =rc_districtSelectDao.selectByDistrict_id(district_id);
        if (district == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        List<Rc_district> li = rc_districtSelectDao.selectByPid(district_id);
        Result<List<Rc_district>> result = new Result<>(ResultEnum.SUCCESS,li);
        return result;
    }

    @Override
    public Result<List<Rc_district>> getAllParentDistrict(short district_id) {
        Rc_district district =rc_districtSelectDao.selectByDistrict_id(district_id);
        if (district == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        List<Rc_district> li = new ArrayList<>();
        li.add(district);
        short pid =  district.getPid();
        while (pid != 0){
            Rc_district parentDistrict = rc_districtSelectDao.selectByDistrict_id(pid);
            li.add(parentDistrict);
            pid = parentDistrict.getPid();
        }

        Result<List<Rc_district>> result = new Result<>(ResultEnum.SUCCESS,li);
        return result;
    }

    @Override
    public Result<String> getAllDistrictName(short district_id){
        Result<List<Rc_district>> listResult =  getAllParentDistrict(district_id);
        if (listResult.getCode()!=200){
            return new Result<>(listResult.getCode(), listResult.getMessage());
        }
        List<Rc_district> li = listResult.getData();
        //foreach对外面变量赋值，原子化或者数组都可以
//        AtomicReference<String> allDistrictName = new AtomicReference<>("");
        String[] allDistrictName = new String[]{""};
        li.forEach(district-> allDistrictName[0] = district.getDistrict() + allDistrictName[0]);
        Result<String> result = new Result<>(ResultEnum.SUCCESS,allDistrictName[0]);
        return result;
    }
}
